<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<script type="text/javascript">

// name space for this page
var p00_svcitem = {
	mid: "${menuId}"
};

// for new item model
Ext.define('p00_svcitem_itemModel',{
	extend:'Ext.data.Model',
	fields: [
        {name: 'svc_item_id',  type: 'string'},
        {name: 'name',   type: 'string'},
        {name: 'price_small', type: 'number'},
        {name: 'price_middle', type: 'number'},
        {name: 'price_big', type: 'number'},
        {name: 'price_high', type: 'number'},
        {name: 'description', type: 'string'},
        {name: 'status', type: 'string'},
        {name: 'status_disp_nm', type: 'string'}
    ]
});

// codition panel
p00_svcitem.condPanel = Ext.create("Ext.form.Panel", {
	region: "north",
    buttonAlign: "left",
    frame: true,
    bodyPadding: 5,
    fieldDefaults: {
    	labelAlign: "left",
        labelWidth: 100,
        width: 240,
        labelStyle: "padding-left: 15px"
    },
    items: [{
        xtype: "fieldset",
    	title: "检索条件",
    	layout: "anchor",
        defaults: {
            padding: "2 0",
            anchor: "100%"
        },
        margin: "0",
    	items: [{
    	    xtype: "container",
            layout: "hbox",
            defaultType: "textfield",
            items: [{
            	id: "p00_svcitem_name",
               	xtype: "textfield",
                fieldLabel: "项目名称",
                name: "name"
            },  {
            	xtype: "uXcombo",
                name: "status",
                comboOpts: {
                    comboId: "CD0006",
                    hasBlank: true
                   // showType:'abbr'    abbr or full
                },
                fieldLabel: "状态",
                labelWidth: 80,
                width: 220,
                triggerAction: "all",
                forceSelection: true,
                editable: false,
               	value:'1'
            }]
        }, {
            xtype: "container",
            layout: "hbox",
            defaultType: "textfield",
            items: []
        }]
    }],
    buttons: [{
        text: "查询",
        id: "p00_svcitem_search",
        handler: p00_svcitem_storeLoad
    }, {
        text: "清空条件",
        handler: function() {
        	this.up("form").getForm().reset();
            Ext.getCmp("p00_svcitem_name").focus();
        }
    },{
    	text: "添加",
        handler: p00_svcitem_showAddItem
    }]
});

// show add item page
function p00_svcitem_showAddItem(){
	p00_svcitem_showItemWin("", "add");
}

// new a panel for add item
//  params svc_item_id id, opentype edit or add
function p00_svcitem_showItemWin(svc_item_id , opentype) {
	var titleName = '新增服务项';
	if('edit' == opentype){
		titleName = '编辑服务项';
	}
	var win;
    if (!win) {
        var form = Ext.widget('form', {
            layout: {
                type: 'vbox',
                align: 'stretch'
            },
            border: false,
            bodyPadding: 20,
            defaults: {
            	xtype: 'textfield',
            	allowBlank: false,
            	labelWidth: 110,
            	width: 200,
            	maxValue: 999999999999,
            	minValue: 0,
			    decimalPrecision: 2,
			    editable: true
		    },
            items: [{
			        name: 'svc_item_id',
			        fieldLabel: '编号',
			        maxLength:'10'
			    },{
			        name: 'name',
			        fieldLabel: '名称',
			        maxLength:'15'
			    },{
			    	xtype:'numberfield',
			        name: 'price_small',
			        fieldLabel: '价格（小型车）'
			    },{
			    	xtype:'numberfield',
			        name: 'price_middle',
			        fieldLabel: '价格（中型车）'
			    },{
			    	xtype:'numberfield',
			        name: 'price_big',
			        fieldLabel: '价格（大型车）'
			    },{
			        xtype:'numberfield',
			        name: 'price_high',
			        fieldLabel: '价格（高级车'
			    },{
			        xtype: "radiogroup",
	                fieldLabel: "状态",
	                column: 2,
	                items:[
                       {boxLabel: '启用',name: 'status', inputValue: '1',checked:true},
                       {boxLabel: '禁用',name: 'status', inputValue: '0'}
	                ],
	               	allowBlank: false,
	               	blankText: '请选择状态'
			    },{
			    	xtype: 'textareafield',
			        name: 'description',
			        fieldLabel: '服务说明',
			        allowBlank: true,
			        height: 100,
			        maxLength: '200'
			    }
            ],
            buttons: [{
                text: '取消',
                handler: function() {
                    this.up('form').getForm().reset();
                    this.up('window').hide();
                }
            }, {
                text: '提交',
                margin: 10,
                handler: submit
            }]
        });

        win = Ext.widget('window', {
            title: titleName,
            closeAction: 'hide',
            padding: 5,
            width: 400,
            minWidth:400,
            height: 450,
            minHeight: 450,
            layout: 'fit',
            resizable: true,
            modal: true,
            items: form
        });
        
       	var urlsub = 'svcitem/add.shtm';
       	var successmsg = '添加成功';
       	var failedmsg = '添加失败';
        if('edit' == opentype){
        	urlsub = 'svcitem/updateitem.shtm?svc_item_id=' + svc_item_id;
        	successmsg = '编辑成功';
       		failedmsg = '编辑失败';
        }
        
        // funciont for submit
        function submit(){
       		var form = this.up('form').getForm();
            var window = this.up('window');
            if (form.isValid()) {
                form.submit({
					// waitMsg:'数据正在提交中',
					url: urlsub,
                    success: function(form, action) {
                       Ext.Msg.alert('Success', successmsg);
                       p00_svcitem_storeLoad();
                       form.reset();
                       window.hide();
                    },
                    failure: function(form, action) {
                        Ext.Msg.alert('Failed', failedmsg);
                    }
                });
            }
        }
        
        //load data
        if('edit' == opentype){
	        form.load({
	        	// waitMsg: '加载数据中',
	        	url: 'svcitem/loadmodel.shtm',
	        	params: {svc_item_id: svc_item_id},
	        	failure:function(form, action){
	        		Ext.Msg.alert('信息提示', '加载失败');
	        	}
	        });
        }
    }
    win.show();
}

//store
p00_svcitem.store = Ext.create("Ext.data.Store", {
	model: p00_svcitem_itemModel,
	proxy: {
	    type: "ajax",
	    url: "svcitem/query.shtm",
	    actionMethods: {
	        read: "POST"
	    },
	    reader: {
	        root: "svcitems",
	        totalProperty: "totalCount"
	    },
	    simpleSortMode: true,
	    listeners: {
	        exception: function(proxy, resp, op, opts) {
	            var obj = Ext.JSON.decode(resp.responseText);
	            Ext.Msg.alert("信息提示", obj.errorMsgs);
	        }
	    }
	}
});

// grid panel
p00_svcitem.gridPanel = Ext.create("Ext.grid.Panel", {
	region: "center",
	frame: true,
	margin:  0,
	autoScroll: true,
    store: p00_svcitem.store,
    columns: [
        { text: "编号", width: 120,  dataIndex: "svc_item_id" },
        { text: "名称", width: 100, dataIndex: "name"},
        { text: "价格（小型车）", width: 150, dataIndex: "price_small"},
        { text: "价格（中型车）", width: 150, dataIndex: "price_middle"},
        { text: "价格（大型车）", width: 150, dataIndex: "price_big"},
        { text: "价格（高级车）", width: 150, dataIndex: "price_high"},
        { text: "状态", width: 80, dataIndex: "status_disp_nm"},
        { text: "服务说明", width: 150, dataIndex: "description"},
        {header: "操作", width: 80, xtype:'actioncolumn', items: [{
	        	icon: crm.global.basePath + 'images/icons/edit.png',
	        	// iconCls: 'p00_svcitemHand',
	        	altText:'编辑',
	        	handler: function(grid, rowIndex, colIndex){
	        		var rec = grid.getStore().getAt(rowIndex);
	        		var svc_item_id = rec.get('svc_item_id');
	        		p00_svcitem_showItemWin(svc_item_id , 'edit');
	        	} 
        	},{
	        	icon: crm.global.basePath + 'images/icons/cross.gif',
	        	// iconCls: 'p00_svcitemHand',
	        	altText:'删除',
	        	handler: function(grid, rowIndex, colIndex){
	        		Ext.MessageBox.confirm('信息提示','确定删除？',function(id){
	        			if(id == 'no'){
		        			return;
	        			}
		        		var rec = grid.getStore().getAt(rowIndex);
		        		var svc_item_id = rec.get('svc_item_id');
		        		var po1_svcitem_requestCon = {
		        			waitMsg: '删除中',
		        			url: 'svcitem/delitem.shtm',
		        			params: {svc_item_id: svc_item_id},
		        			callback: function(options, success, response){
		        				if(true == success){
		        					// Ext.Msg.alert("信息提示", "删除成功");
		        					p00_svcitem_storeLoad();
		        				}else{
		        					Ext.Msg.alert('信息提示', "删除失败");
		        				}
		        				
		        			}
		        		};
		        		Ext.Ajax.request(po1_svcitem_requestCon);
	        		});
	        	}
        	}]
        }
    ],
    viewConfig: {
    	forceFit: true
    }
});

// add to pTab
Ext.getCmp(p00_svcitem.mid).add({
	xtype: "panel",
	bodyPadding: 0,
	border: 0,
	layout: "border",
	margin: 0,
	items: [p00_svcitem.condPanel, p00_svcitem.gridPanel]
});
//set "name" on focus.
Ext.getCmp("p00_svcitem_name").focus();

p00_svcitem_storeLoad();

// load the item store
function p00_svcitem_storeLoad(){
	p00_svcitem.store.load({
    		params: p00_svcitem.condPanel.getForm().getValues()
    });
}
</script>

